<template>
	<view class="page-login uni-padding-wrap">
		<view class="app-plus">
			<view class="account-login">
				<form @submit="formSubmit">
					<view class="uni-form-item uni-column account">
						<input class="uni-input" name="account" placeholder="邮箱" @input="accountInput" />
					</view>
					<view class="uni-form-item uni-column password">
						<input class="uni-input" name="password" placeholder="密码" @input="passwordInput" />
					</view>
					<view class="uni-btn-v">
						<button formType="submit" type="primary">登录</button>
					</view>
					<view class="go-register-wrap">
						<text>没有账号？</text>
						<text class="go-register" @tap="goRegister">去注册</text>
					</view>
				</form>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				options: {},
				account: "",
				password: "",
			};
		},
		onLoad(options) {
			console.log(options);
			this.options = options
			this.$store.commit('setNeedFreshPage', true)
		},
		onUnload() {
			console.log(`page: login, setNowInLoginPage: false`)
			this.$store.commit('setNowInLoginPage', false)
		},
		methods: {
			async registerByWeixinMiniProgram(e) {
				console.log(e);
				this.options.iv = e.detail.iv
				this.options.encryptedData = e.detail.encryptedData
				let result = await this.$api.registerByWeixinMiniProgram(this.options)
				console.log(result)
				if (result.ret == 0) {
					this.$setUser(result.data);
					// uni.navigateBack({
					// 	delta: 1
					// })
					uni.switchTab({
						url: "/pages/index/index"
					})
				}
			},
			accountInput(e) {
				this.account = e.target.value
			},
			passwordInput(e) {
				this.password = e.target.value
			},
			async formSubmit(e) {
				console.log(e);
				let result = await this.$api.loginByAccountPassword(e.detail.value)
				console.log(result)
				if (result.ret == 0) {
					this.$setUser(result.data);
					// uni.navigateBack({
					// 	delta: 1
					// })
					uni.switchTab({
						url: "/pages/index/index"
					})
				}
			},
			goRegister(e) {
				uni.navigateTo({
					url: '/pages/register/register'
				})
			}
		}
	}
</script>

<style lang="scss">
.page-login {
	.weixin-login {
		margin: 40upx 0;
		.btn-submit {
			margin: 40upx 0;
		}
	}
	.account-login {
		padding: 100upx 0;
		.account {
			border-bottom: 1upx solid #ddd;
		},
		.password {
		}
		.submit-wrap {
		}
	}
	.go-register-wrap {
		display: flex;
		flex-direction: row;
		justify-content: center;
		.go-register {
			color: blue;
		}
	}
}

</style>
